package com.ews.onlineexchange.controller.pub;

import com.ews.onlineexchange.model.Advice;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.service.EntityQuery;
import org.apache.commons.lang.StringUtils;

import javax.enterprise.inject.Model;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * @program: SdewsOnlineExchange_dys
 * @description:
 * @author: wangchangqing
 * @create: 2021-03-11 11:23
 **/
@Model
public class AdviceList extends EntityQuery<Advice> {
    private String name;
    private String phone;
    private String title;
    @Override
    protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Advice> entity) {
        List<Predicate> restrictions = super.getRestrictions(cb, entity);
        restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
        if(StringUtils.isNotBlank(name)){
            restrictions.add(cb.like(entity.get("name"), getStringRestriction(name)));
        }
        if(StringUtils.isNotBlank(phone)){
            restrictions.add(cb.like(entity.get("phone"), getStringRestriction(phone)));
        }
        if(StringUtils.isNotBlank(title)){
            restrictions.add(cb.like(entity.get("title"), getStringRestriction(title)));
        }
        return restrictions;
    }

    public AdviceList() {
        setMaxResults(10);
        setOrder("createDate DESC,id");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
